#region Using Directives

using System;
using Eneta.Portal.Forum.Common.Data;

#endregion

namespace Eneta.Portal.Forum.Core.Data
{
    public class ForumMapper
    {
        /// <summary>
        /// Creates the dto.
        /// </summary>
        /// <param name="forum">The forum.</param>
        /// <returns></returns>
        public static SharePointListItem CreateDto(Domain.Entities.Forum forum)
        {
            string[] values = {
                                  "Title", forum.Name,
                                  "Description", forum.Description,
                                  "CategoryID", forum.CategoryId.ToString(),
                                  "TopicCount", forum.TopicCount.ToString(),
                                  "PostCount", forum.PostCount.ToString(),
                              };

            return new SharePointListItem(forum.Id, values);
        }

        /// <summary>
        /// Creates the domain object.
        /// </summary>
        /// <param name="listItem">The list item.</param>
        /// <returns></returns>
        public static Domain.Entities.Forum CreateDomainObject(SharePointListItem listItem)
        {
            int categoryId = Convert.ToInt32(listItem["CategoryID"]);
            var forum = new Domain.Entities.Forum(listItem.Id, categoryId, listItem["Title"]);

            forum.Description = listItem["Description"];
            if(listItem["TopicCount"] == null || listItem["TopicCount"] == string.Empty)
                forum.TopicCount = 0;
            else
                forum.TopicCount = Convert.ToInt32(listItem["TopicCount"]);

            if(listItem["PostCount"] == null || listItem["PostCount"] == string.Empty)
                forum.PostCount = 0;
            else
                forum.PostCount = Convert.ToInt32(listItem["PostCount"]);

            // TODO I think this is wrong. The last post should be set when
            // a topic is added, not modified
            forum.LastPost = Convert.ToDateTime(listItem["Modified"]);

            return forum;
        }
    }
}